home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / language / ici / ici.cpi / control.c < prev    next >
C/C++ Source or Header  |  1994-10-27  |  4KB  |  189 lines

  1. #include "exec.h"
  2. #include "op.h"
  3. #include "int.h"
  4. #include "buf.h"
  5. #include "pc.h"
  6. #include "struct.h"
  7. #include "null.h"
  8. #include "forall.h"
  9. #include "catch.h"
  10.  
  11. /*
  12.  * self    => array looper &array[2] (xs)
  13.  * array => - (os)
  14.  *
  15.  * Ie, like a loop, but on the first time in it skips the first two elements
  16.  * of the the loop, which are expected to be an array of code for the step
  17.  * and an exec operator to run it.
  18.  */
  19. int
  20. op_for()
  21. {
  22.     pc_t    *pc;
  23.  
  24.     if ((pc = new_pc(arrayof(o_top[-1]))) == NULL)
  25.     return 1;
  26.     pc->pc_next += opof(x_top[-1])->op_code;
  27.     x_top[-1] = o_top[-1];
  28.     *x_top++ = objof(&o_looper);
  29.     *x_top++ = objof(pc);
  30.     --o_top;
  31.     return 0;
  32. }
  33.  
  34. /*
  35.  * obj self    => obj self pc (xs)
  36.  *        => (os)
  37.  */
  38. int
  39. op_looper()
  40. {
  41.     if ((*x_top = objof(new_pc(arrayof(x_top[-2])))) == NULL)
  42.     return 1;
  43.     ++x_top;
  44.     return 0;
  45. }
  46.  
  47. /*
  48.  * Pop the execution stack until a looper is found.
  49.  */
  50. STATIC int
  51. op_continue()
  52. {
  53.     object_t    **s;
  54.  
  55.     for (s = xs->a_top; s > xs->a_base + 1; --s)
  56.     {
  57.     if (iscatch(s[-1]) && catchof(s[-1])->c_catcher == objof(&o_null))
  58.         break;
  59.     if (s[-1] == objof(&o_looper) || isforall(s[-1]))
  60.     {
  61.         x_top = s;
  62.         return 0;
  63.     }
  64.     }
  65.     error = "continue not within loop";
  66.     return 1;
  67. }
  68.  
  69. /*
  70.  * bool obj => bool (os) OR pc (xs)
  71.  */
  72. STATIC int
  73. op_andand()
  74. {
  75.     int        c;
  76.  
  77.     if ((c = !isfalse(o_top[-2])) == opof(x_top[-1])->op_code)
  78.     {
  79.     /*
  80.      * Have to test next part of the condition.
  81.      */
  82.     if ((*--x_top = objof(new_pc(arrayof(o_top[-1])))) == NULL)
  83.         return 1;
  84.     o_top -= 2;
  85.     ++x_top;
  86.     return 0;
  87.     }
  88.     /*
  89.      * Reduce the value to 0 or 1.
  90.      */
  91.     o_top[-2] = objof(c ? o_one : o_zero);
  92.     --o_top;
  93.     --x_top;
  94.     return 0;
  95. }
  96.  
  97. /*
  98.  * bool obj => -
  99.  */
  100. STATIC int
  101. op_if()
  102. {
  103.     if (isfalse(o_top[-2]))
  104.     {
  105.     o_top -= 2;
  106.     --x_top;
  107.     return 0;
  108.     }
  109.     if ((*--x_top = objof(new_pc(arrayof(o_top[-1])))) == NULL)
  110.     return 1;
  111.     ++x_top;
  112.     o_top -= 2;
  113.     return 0;
  114. }
  115.  
  116. /*
  117.  * bool obj1 obj2 => -
  118.  */
  119. STATIC int
  120. op_ifelse()
  121. {
  122.     --x_top;
  123.     if ((*x_top = objof(new_pc(arrayof(o_top[-1 - !isfalse(o_top[-3])])))) == NULL)
  124.     return 1;
  125.     ++x_top;
  126.     o_top -= 3;
  127.     return 0;
  128. }
  129.  
  130. /*
  131.  * NULL self (xs) =>
  132.  */
  133. STATIC int
  134. op_switcher()
  135. {
  136.     x_top -= 2;
  137.     return 0;
  138. }
  139.  
  140. /*
  141.  * value array struct    => (os)
  142.  *            => NULL switcher (pc(array) + struct.value) (xs)
  143.  */
  144. STATIC int
  145. op_switch()
  146. {
  147.     register slot_t    *sl;
  148.  
  149.     if ((sl = find_raw_slot(structof(o_top[-1]), o_top[-3]))->sl_key == NULL)
  150.     {
  151.     if ((sl = find_raw_slot(structof(o_top[-1]), objof(&o_mark)))->sl_key == NULL)
  152.     {
  153.         o_top -= 3;
  154.         --x_top;
  155.         return 0;
  156.     }
  157.     }
  158.     x_top[-1] = objof(&o_null);
  159.     *x_top++ = objof(&o_switcher);
  160.     if ((*x_top = objof(new_pc(arrayof(o_top[-2])))) == NULL)
  161.     return 1;
  162.     pcof(*x_top)->pc_next += intof(sl->sl_value)->i_value;
  163.     ++x_top;
  164.     o_top -= 3;
  165.     return 0;
  166. }
  167.  
  168. STATIC int
  169. op_pop()
  170. {
  171.     --o_top;
  172.     --x_top;
  173.     return 0;
  174. }
  175.  
  176. op_t    o_exec        = {OBJ(TC_OP, op_type), NULL, OP_EXEC};
  177. op_t    o_looper    = {OBJ(TC_OP, op_type), op_looper};
  178. op_t    o_loop        = {OBJ(TC_OP, op_type), NULL, OP_LOOP};
  179. op_t    o_break        = {OBJ(TC_OP, op_type), NULL, OP_BREAK};
  180. op_t    o_continue    = {OBJ(TC_OP, op_type), op_continue};
  181. op_t    o_if        = {OBJ(TC_OP, op_type), op_if};
  182. op_t    o_ifnotbreak    = {OBJ(TC_OP, op_type), NULL, OP_IFNOTBREAK};
  183. op_t    o_ifelse    = {OBJ(TC_OP, op_type), op_ifelse};
  184. op_t    o_pop        = {OBJ(TC_OP, op_type), op_pop};
  185. op_t    o_andand    = {OBJ(TC_OP, op_type), op_andand, 0, 1};
  186. op_t    o_barbar    = {OBJ(TC_OP, op_type), op_andand, 0, 0};
  187. op_t    o_switch    = {OBJ(TC_OP, op_type), op_switch};
  188. op_t    o_switcher    = {OBJ(TC_OP, op_type), op_switcher};
  189.